// 假设 cw_bat 是一个结构体，定义如下
typedef struct {
    int usb_online;
} cw_bat_t;

// 声明 cw_bat 变量
cw_bat_t cw_bat;

#include <stdio.h>
#include "driver/i2c.h"
#include "esp_log.h"
#include "cw2015.h"

#define I2C_SDA_PIN GPIO_NUM_5
#define I2C_SCL_PIN GPIO_NUM_4
#define I2C_MASTER_NUM I2C_NUM_0  // I2C端口号
#define I2C_MASTER_FREQ_HZ 50000  // 降至50KHz
#define I2C_TIMEOUT_MS 1000       // 超时时间：1秒

static const char *TAG = "CW2015";

// I2C初始化函数
static esp_err_t i2c_master_init(void)
{
    i2c_config_t conf = {
        .mode = I2C_MODE_MASTER,
        .sda_io_num = I2C_SDA_PIN,
        .scl_io_num = I2C_SCL_PIN,
        .sda_pullup_en = GPIO_PULLUP_ENABLE,
        .scl_pullup_en = GPIO_PULLUP_ENABLE,
        .master.clk_speed = I2C_MASTER_FREQ_HZ,
    };

    esp_err_t err = i2c_param_config(I2C_MASTER_NUM, &conf);
    if (err != ESP_OK)
    {
        return err;
    }

    return i2c_driver_install(I2C_MASTER_NUM, conf.mode, 0, 0, 0);
}

// I2C设备扫描函数
static void i2c_scanner(void)
{
    uint8_t address;
    printf("扫描I2C总线...\n");

    uint8_t devices_found = 0;

    for (address = 1; address < 127; address++)
    {
        i2c_cmd_handle_t cmd = i2c_cmd_link_create();
        i2c_master_start(cmd);
        i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, true);
        i2c_master_stop(cmd);

        esp_err_t ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, I2C_TIMEOUT_MS / portTICK_PERIOD_MS);
        i2c_cmd_link_delete(cmd);

        if (ret == ESP_OK)
        {
            printf("找到I2C设备，地址: 0x%02x\n", address);
            devices_found++;
        }
    }

    if (devices_found == 0)
    {
        printf("未找到I2C设备\n");
    }
    else
    {
        printf("扫描完成，共找到 %d 个设备\n", devices_found);
    }
}

void app_main(void)
{
    ESP_LOGI(TAG, "I2C扫描器启动");

    // 初始化I2C
    esp_err_t ret = i2c_master_init();
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "I2C初始化失败: %d", ret);
        return;
    }
    
    // 先运行I2C扫描
    i2c_scanner();
    
    // 初始化CW2015
    ret = cw2015_init();
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "CW2015初始化失败: %d", ret);
        return;
    }
    
    while (1) {
        float voltage, soc;
        uint16_t remaining_minutes;
        
        // 读取电池信息
        if (cw2015_get_voltage(&voltage) == ESP_OK) {
            ESP_LOGI(TAG, "电池电压: %.3fV (标称3.7V)", voltage/1000.0);
            // 计算当前功率
            float power = (voltage/1000.0) * (3000.0 * (soc/100.0)) / 1000.0; // 瓦特
            ESP_LOGI(TAG, "当前电池能量: %.2fWh (总能量11.1Wh)", power);
        } else {
            ESP_LOGE(TAG, "读取电压失败");
        }
        
        if (cw2015_get_soc(&soc) == ESP_OK) {
            ESP_LOGI(TAG, "电池电量: %.3f%% (容量3000mAh)", soc);
            // 计算剩余容量
            float remaining_capacity = 3000.0 * (soc/100.0);
            ESP_LOGI(TAG, "剩余容量: %.0fmAh/3000mAh", remaining_capacity);
        } else {
            ESP_LOGE(TAG, "读取电量失败");
        }
        
        if (cw2015_get_remaining_time(&remaining_minutes) == ESP_OK) {
            if (remaining_minutes > 0) {
                ESP_LOGI(TAG, "预计剩余时间: %d小时%d分钟 (估算值)", 
                         remaining_minutes/60, remaining_minutes%60);
                
                // 显示预计可用时间
                float hours = remaining_minutes / 60.0f;
                if (hours > 24) {
                    ESP_LOGI(TAG, "预计可用超过1天");
                } else {
                    ESP_LOGI(TAG, "预计可用%.1f小时", hours);
                }
            } else {
                ESP_LOGI(TAG, "电池电量过低或正在充电");
            }
        } else {
            ESP_LOGW(TAG, "无法获取剩余时间信息");
        }
        
        // 添加充电状态检测
        float voltage1, voltage2;
        if (cw2015_get_voltage(&voltage1) == ESP_OK) {
            vTaskDelay(pdMS_TO_TICKS(1000));
            if (cw2015_get_voltage(&voltage2) == ESP_OK) {
                if (voltage2 > voltage1) {
                    cw_bat.usb_online = 1;
                    ESP_LOGI(TAG, "检测到充电中");
                } else {
                    cw_bat.usb_online = 0;
                }
            }
        }
        
        vTaskDelay(pdMS_TO_TICKS(5000));
    }
}
